package pearson.automation.forms;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.RemoteWebElement;

import pearson.automation.forms.note.NewNoteForm;

import framework.BaseForm;
import framework.elements.Button;
import framework.elements.ElementList;
import framework.elements.Label;

public class SectionContents extends BaseForm {

    private static Label identifier = new Label(By.className("module-title"), "Section"); 

    private Button sideBarToggle = new Button(By.id("edit-sidebar-toggler"), "Sidebar toggle");
    private Label sideBarElement = new Label(By.id("edit-sidebar"), "Sidebar");
    
    private Button newParagraph = new Button(By.tagName("em"), "New Paragraph button");
    private Label  preLastParagraph = new Label(By.xpath("(//p)[last()-1]"), "Pre-last Paragraph");
    private Button newNote      = new Button(By.xpath("(//p)[last()-1]/div"), "New note for pre-last paragraph");
    
    private Label  firstAsset = new Label(By.xpath("(//div[contains(@class,'document')])[1]"), "First asset");
    private Button firstAssetNewNote = new Button(By.xpath("(//div[contains(@class,'document')])[1]/div[@class='note_icon']"), "First asset");
    
    private Label  objectives = new Label(By.xpath("//div[@class='section_content']/ul"), "Objectives");
    private Button objectivesNewNote = new Button(By.xpath("//div[@class='section_content']/ul//div"), "New note for objectives");
    
    private ElementList assets = new ElementList(
            By.xpath("//p[@data-type='title']"), "Elements"); 
    
    private String imageAssetXPathTemplate = "//img[@data-type='imagedata'][%d]"; 
    
    private String imageOnEditTemplate = "(//img[@data-damid='%s'])[1]";
    private String imageOnPreviewTemplate = "(//img[@data-damid='%s'])[2]";
    
    public SectionContents() {
        super(identifier.getLocator(), "Section");
    }
    
    public RemoteWebElement getContentsElement() {
        return newParagraph.getElement();
    }
    
    public String getEmbeddedAssetTitle(int index) {
        return assets.getListElement(index);
    }
    
    public String getImageAssetTitle(int index) {
        Label image = new Label (By.xpath(
                String.format(imageAssetXPathTemplate, index)),
                "Thumbnail");
        return image.getElement().getAttribute("alt");
    }
    
    public void clickPreviewButtonForFirstVisibleAsset() {
        List<WebElement> list = browser.getDriver().findElements(
                By.xpath("//a[@class='preview']"));
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).isDisplayed()) {
                list.get(i).click();
                break;
            }
        }
    }
    
    public NewNoteForm clickAddNoteToPreLastParagraph() {    
        Actions builder = new Actions(browser.getDriver());
        builder.moveToElement(preLastParagraph.getElement()).build().perform();
        
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        
        builder
            .moveToElement(preLastParagraph.getElement())
            .moveToElement(preLastParagraph.getElement())
            .moveToElement(browser.getDriver().findElement(newNote.getLocator()))
            .click(browser.getDriver().findElement(newNote.getLocator()))
            .build().perform();
        return new NewNoteForm();
    }

    public void addParagraph(String string) {
        newParagraph.clickAndWait();
        WebElement activeElement = browser.getDriver().switchTo().activeElement();
        activeElement.sendKeys(string, Keys.ENTER);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        identifier.clickAndWait();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public RightHandPanel openRightHandPanel() {
        if (!sideBarElement.getElement().getCssValue("right").equals("0px")) {
            sideBarToggle.clickAndWait();
            // Wait for element to settle
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return new RightHandPanel();
    }

    public NewNoteForm clickAddNoteToLastAsset() {
        Actions builder = new Actions(browser.getDriver());
        builder.moveToElement(firstAsset.getElement()).build().perform();
        
        builder
            .moveToElement(firstAsset.getElement())
            .moveToElement(browser.getDriver().findElement(firstAssetNewNote.getLocator()))
            .click(browser.getDriver().findElement(firstAssetNewNote.getLocator()))
            .build().perform();
        return new NewNoteForm();
    }
    
    public NewNoteForm clickAddNoteToObjectives() {
        Actions builder = new Actions(browser.getDriver());
        builder.moveToElement(objectives.getElement()).build().perform();
        
        builder
            .moveToElement(objectives.getElement())
            .moveToElement(browser.getDriver().findElement(
                    objectivesNewNote.getLocator()))
            .click(browser.getDriver().findElement(
                    objectivesNewNote.getLocator()))
            .build().perform();
        return new NewNoteForm();
    }

    public void performAMouseOverForFirstAsset(String damid, boolean onPreview) {
        Label label = null;
        if (onPreview) {
            label = new Label (By.xpath(String.format(
                    imageOnPreviewTemplate, damid)), "Image");
        } else {
            label = new Label (By.xpath(String.format(
                    imageOnEditTemplate, damid)), "Image");
        }
        Actions builder = new Actions(browser.getDriver());
        builder.moveToElement(label.getElement()).build().perform();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public String getImageDamid(int index) {
        Label image = new Label (By.xpath(
                String.format(imageAssetXPathTemplate, index)),
        "Thumbnail");
        return image.getElement().getAttribute("data-damid");
    }
}
